'use strict';
layui.define(['jquery','laytpl'], function(exports){
    let $ = layui.jquery
        ,laytpl = layui.laytpl;

    let obj = {
        box: null,
        shift: null,
        data : null,

        page : 1,
        limit : 7,
        tpl: '',
        //初始化
        init: function(conf){

            this.data = conf.data;
            this.box = $(conf.box);
            this.shift = $(conf.shift);
            this.limit = conf.limit;
            this.tpl = conf.tpl;

            //判断是否隐藏按钮
            if (this.data.length <= this.limit){
                this.shift.hide();
            }
            this.event();
            this.refresh();
        },
        //添加事件
        event : function(){
            let angle = 0
                ,that = this;
            this.shift.on('click', function(){
                angle += 360;
                $(this).css({
                    'transform':'rotate('+angle+'deg)',
                    '-ms-transform':'rotate('+angle+'deg)',
                    '-moz-transform':'rotate('+angle+'deg)',
                    '-webkit-transform':'rotate('+angle+'deg)',
                    '-o-transform':'rotate('+angle+'deg)',
                });
                that.refresh();
            });
        },
        //刷新数据
        refresh: function(){
            let length = this.data.length
                ,totalPage = Math.ceil(length / this.limit)
                ,data = this.data.slice((this.page - 1)*this.limit,this.page * this.limit);

            if (this.page >= totalPage){
                this.page = 1;
            }else{
                this.page += 1;
            }

            //渲染数据
            this.render(data);
        },
        //渲染html
        render:function(d){
            let that = this;
            laytpl(that.tpl).render(d, function(html){

                //删除兄弟元素 追加到前面
                that.shift.siblings().remove();
                that.shift.before(html);
            });
        }

    };


    //输出test接口
    exports('shift', obj);
});